package cn.gson.crm.model.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.mysql.jdbc.StringUtils;

import cn.gson.crm.model.Jdbc;
import cn.gson.crm.model.entity.ReceiptService;

public class RecordDao {
	private int pageSize=7;
	
	/**
	 * 查看收款记录
	 */
	public List<ReceiptService> getReceiptRecord(int page, String staffName,String stuId)
			throws SQLException {
		int start = (page - 1) * pageSize;
		String sql = "SELECT * FROM receipt LEFT JOIN total ON receipt.receipt_total_id=total.total_id INNER JOIN student ON total.total_student_id=student.student_id INNER JOIN staff ON receipt.receipt_staff_id=staff.staff_id where student.student_id = ?";
		List<Object> params = new ArrayList<>();
		System.out.println(staffName+"*********"+stuId);
		params.add(stuId);
		//params.add(totalStaffId);
		sql = buildSqlAndParams(params, sql, staffName);
		sql += " order by receipt.receipt_id desc limit ?,?";
		//System.out.println(stuId+"***********"+totalStaffId);
		params.add(start);
		params.add(pageSize);
		System.out.println(sql);
		return queryUserList(sql, params.toArray());
	}
	
	/**
	 * 搜索总页数
	 */
	public int getTotalServicePage(String staffName,String stuId) throws SQLException {
		//String sql = "select count(*) totals from total INNER JOIN student ON total.total_student_id = student.student_id LEFT JOIN `group` b ON total.total_student_id = b.group_student_id INNER JOIN clazz c ON b.group_clazz_id = c.clazz_id";
		String sql = "SELECT count(*) totals FROM receipt LEFT JOIN total ON receipt.receipt_total_id=total.total_id INNER JOIN student ON total.total_student_id=student.student_id INNER JOIN staff ON receipt.receipt_staff_id=staff.staff_id where student.student_id = ?";
		List<Object> params = new ArrayList<>();
		params.add(stuId);
		sql = buildSqlAndParams(params, sql, staffName);
		Map<String, Object> map = Jdbc.getInstace().executeQuery(sql, params.toArray());
		Integer TotalService = Integer.parseInt(map.get("totals").toString());
		return (int) Math.ceil(TotalService.doubleValue() / pageSize);
	}
	
	private String buildSqlAndParams(List<Object> params, String sql,String staffName) {
		//params.add(status);
		if (!StringUtils.isNullOrEmpty(staffName)) {
			sql += " and staff.staff_name like ? ";
			params.add("%" + staffName + "%");
		}
		return sql;
	}
	/*private String buildSqlAndParamstwo(List<Object> params, String sql,String studentName) {
		//params.add(status);
		if (!StringUtils.isNullOrEmpty(studentName)) {
			sql += " and staff.staff_name like ? ";
			params.add("%" + studentName + "%");
		}

		return sql;
	}*/
	
	private List<ReceiptService> queryUserList(String sql, Object... params) throws SQLException {
		List<Map<String, Object>> listData = Jdbc.getInstace().executeQueryForList(sql, params);
		/*List<TotalService> TotalServiceList = null;
		if (listData != null && listData.size() > 0) {
			TotalServiceList = new ArrayList<>();
			for (Map<String, Object> data : listData) {
				TotalServiceList.add(buildTotalService(data));
			}
		}*/
		return changeReceipt(listData);
	}
	
	public List<ReceiptService> changeReceipt(List<Map<String,Object>>list){
		List<ReceiptService>receiptList=null;
		if (list!=null&&list.size()>0) {
			receiptList=new ArrayList<>();
			for (Map<String,Object> map : list) {
				String d=map.get("receipt_date").toString().split(" ")[0];
				receiptList.add(new ReceiptService(
						Integer.parseInt(map.get("receipt_id").toString()),
						Double.parseDouble(map.get("receipt_money").toString()),
						Integer.parseInt(map.get("receipt_staff_id").toString()),
						Integer.parseInt(map.get("receipt_total_id").toString()),
						java.sql.Date.valueOf(d),
						(String)map.get("student_name"),
						(String)map.get("staff_name")
						));
			}
		}
		return receiptList;
		
	}
}
